﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using NIEN_LUAN.Db;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormTimKiemPhieuThu : Form
    {
        private DataContentDataContext db = new DataContentDataContext();

        public FormTimKiemPhieuThu()
        {
            InitializeComponent();
        }

        private void btnTimPT_Click(object sender, EventArgs e)
        {
            if (this.checkBoxTimkiemSoCT.Checked && !this.checkBoxTkiemMNV.Checked && !this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.comboBoxCT.Text))
                {
                    MessageBox.Show("Chưa nhập số chứng từ", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                var phieuthu = (from m in db.PHIEUTHUs
                                where m.SoChungTu == this.comboBoxCT.Text
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (!this.checkBoxTimkiemSoCT.Checked && this.checkBoxTkiemMNV.Checked && !this.checkBoxTkiemNgaylap.Checked)
            {               
                if (string.IsNullOrEmpty(this.comboBoxNV.Text))
                {
                    MessageBox.Show("Chưa nhập mã Nhân viên");
                    return;
                }

                var phieuthu = (from m in db.PHIEUTHUs
                                where m.MaNhanVien == this.comboBoxNV.Text
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (!this.checkBoxTimkiemSoCT.Checked && !this.checkBoxTkiemMNV.Checked && this.checkBoxTkiemNgaylap.Checked)
            {               
                var phieuthu = (from m in db.PHIEUTHUs
                                where ((m.NgayLap >= this.dateTimePickerNgay1.Value) && (m.NgayLap <= this.dateTimePickerNgay2.Value))
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (!this.checkBoxTimkiemSoCT.Checked && this.checkBoxTkiemMNV.Checked && this.checkBoxTkiemNgaylap.Checked)
            {               
                if (string.IsNullOrEmpty(this.comboBoxNV.Text))
                {
                    MessageBox.Show("Chưa nhập mã Nhân viên");
                    return;
                }

                var phieuthu = (from m in db.PHIEUTHUs
                                where ((m.MaNhanVien == this.comboBoxNV.Text) && ((m.NgayLap >= this.dateTimePickerNgay1.Value) && (m.NgayLap <= this.dateTimePickerNgay2.Value)))
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (this.checkBoxTimkiemSoCT.Checked && !this.checkBoxTkiemMNV.Checked && this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.comboBoxCT.Text))
                {
                    MessageBox.Show("Chưa nhập số chứng từ", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                var phieuthu = (from m in db.PHIEUTHUs
                                where ((m.SoChungTu == this.comboBoxCT.Text) && ((m.NgayLap >= this.dateTimePickerNgay1.Value) && (m.NgayLap <= this.dateTimePickerNgay2.Value)))
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (this.checkBoxTimkiemSoCT.Checked && this.checkBoxTkiemMNV.Checked && !this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.comboBoxCT.Text))
                {
                    MessageBox.Show("Chưa nhập số chứng từ", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (string.IsNullOrEmpty(this.comboBoxNV.Text))
                {
                    MessageBox.Show("Chưa nhập mã Nhân viên");
                    return;
                }

                var phieuthu = (from m in db.PHIEUTHUs
                                where ((m.SoChungTu == this.comboBoxCT.Text) && (m.MaNhanVien == this.comboBoxNV.Text))
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (this.checkBoxTimkiemSoCT.Checked && this.checkBoxTkiemMNV.Checked && this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.comboBoxCT.Text))
                {
                    MessageBox.Show("Chưa nhập số chứng từ", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (string.IsNullOrEmpty(this.comboBoxNV.Text))
                {
                    MessageBox.Show("Chưa nhập mã Nhân viên");
                    return;
                }                

                var phieuthu = (from m in db.PHIEUTHUs
                                 where ((m.SoChungTu == this.comboBoxCT.Text) && (m.MaNhanVien == this.comboBoxNV.Text) && ((m.NgayLap >= this.dateTimePickerNgay1.Value) && (m.NgayLap <= this.dateTimePickerNgay2.Value)))
                                 select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (!this.checkBoxTimkiemSoCT.Checked && !this.checkBoxTkiemMNV.Checked && !this.checkBoxTkiemNgaylap.Checked)
            {
                var phieuthu = (from m in db.PHIEUTHUs
                               select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
            }
        }

        private void btnSuaPT_Click(object sender, EventArgs e)
        {
            Form frmSuaPT = new FormChiTietPhieuThu();
            frmSuaPT.ShowDialog();
        }

        private void btnThoatPT_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChonKH_Click(object sender, EventArgs e)
        {
            Form frmChonKH = new FormTimChiTietKhachHang();
            frmChonKH.ShowDialog();
        }

        private void btnchonNV_Click(object sender, EventArgs e)
        {
            Form frmChonNV = new FormTimKiemNhanVien(this );
            frmChonNV.ShowDialog();
        }

        private void FormTimKiemPhieuThu_Load(object sender, EventArgs e)
        {
            this.dataGridViewPThu.AutoGenerateColumns = false;
            var phieuthu = (from pt in db.PHIEUTHUs
                            orderby pt.SoPhieuThu ascending
                            select pt
                                ).ToList();
            this.pHIEUTHUBindingSource.DataSource = phieuthu;

            var nv = (from m in db.NHANVIENs
                      select m).ToList();
            this.bsMaNV.DataSource = nv;
            this.comboBoxNV.DataSource = bsMaNV;
            this.comboBoxNV.DisplayMember = "MaNhanVien";

            var ct = (from m in db.CHUNGTUs
                      select m).ToList();
            this.bsSoct.DataSource = ct;
            this.comboBoxCT.DataSource = bsSoct;
            this.comboBoxCT.DisplayMember = "SoChungTu"; 
        }

        private void btnChonSoCT_Click(object sender, EventArgs e)
        {
            //Form frmChonCT = new FormTimKiemChungTu(this);
            //frmChonCT.ShowDialog();
        }

        private void btnXoaPT_Click(object sender, EventArgs e)
        {
            PHIEUTHU pt = pHIEUTHUBindingSource.Current as PHIEUTHU;
            if (pt != null)
            {
                var tl = MessageBox.Show("Bạn có chắc chắn xóa", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (tl == DialogResult.Yes)
                {
                //    var chitietpt = (from m in db.PHIEUTHUs
                //                     where m.SoPhieuThu == pt.SoPhieuThu
                //                     select m).ToList();

                    try
                    {
                        //db.PHIEUTHUs.DeleteAllOnSubmit(chitietct);
                        db.PHIEUTHUs.DeleteOnSubmit(pt);
                        db.SubmitChanges();
                        this.pHIEUTHUBindingSource.RemoveCurrent();
                        MessageBox.Show("Đã xóa ");

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Đã có lỗi khi thực hiện điều này :)) LỖi chi tiết : " + ex.Message);
                        return;
                        //throw;
                    }
                }
            }
            else
            {
                MessageBox.Show("Chọn chứng từ");
            }
        }

        private void btnChonSoCT_Click_1(object sender, EventArgs e)
        {
            Form frmChonCT = new FormTimKiemChungTu(this);
            frmChonCT.ShowDialog();
        }

        private void btnXemPT_Click(object sender, EventArgs e)
        {
            var phieuthu = this.pHIEUTHUBindingSource.Current as PHIEUTHU;
            if (phieuthu != null)
            {
                Form frmXemPT = new FormLapPhieuThu(this, phieuthu);
                frmXemPT.ShowDialog();
            }
            else
            {
                MessageBox.Show("Chọn chứng từ");
            }
        }
    }
}
